package com.yang.seckillsys.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.yang.seckillsys.pojo.Order;
import com.yang.seckillsys.pojo.SeckillGoods;
import com.yang.seckillsys.pojo.SeckillOrder;
import com.yang.seckillsys.pojo.User;
import com.yang.seckillsys.service.IGoodsService;
import com.yang.seckillsys.service.IOrderService;
import com.yang.seckillsys.service.ISeckillGoodsService;
import com.yang.seckillsys.service.ISeckillOrderService;
import com.yang.seckillsys.vo.GoodsVo;
import com.yang.seckillsys.vo.RespBeanEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author YANGLIYUAN
 * @since 2021-04-17
 */

@Controller
@Slf4j
@RequestMapping("/seckill")
public class SeckillOrderController {
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private ISeckillOrderService seckillOrderService;
    @Autowired
    private IOrderService orderService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ISeckillGoodsService seckillGoodsService;
    @RequestMapping("/doseckill")
    public String doSeckill(Model model, User user, Long goodsId) {
        if (user == null) {
            return "login";
        }
        model.addAttribute("user", user);
        if(redisTemplate.opsForValue().get(goodsId+":")==null){
            redisTemplate.opsForValue().set(goodsId+":",0);
        }
        GoodsVo goods = goodsService.findGoodsByGoodsId(goodsId);
        //判断redis中库存
        Integer redis_stock= (Integer) redisTemplate.opsForValue().get(goodsId+":");
        if (redis_stock>0){
            //判断是否重复抢购
        /*SeckillOrder seckillOrder = seckillOrderService.getOne(new
                QueryWrapper<SeckillOrder>().eq("user_id", user.getId()).eq(
                "goods_id", goodsId));*/
            SeckillOrder seckillOrder = (SeckillOrder) redisTemplate.opsForValue().get("order:"+user.getId()+":"+goods.getId());
            if (seckillOrder != null) {
                model.addAttribute("errmsg", RespBeanEnum.REPEATE_ERROR.getMessage());
                return "seckillFail";
            }
            Order order = orderService.seckill(user, goods,model);
            model.addAttribute("order", order);
            model.addAttribute("goods", goods);
        }else {
            //redis中库存小于0，查询数据库进行预取
            //redis预取库存，库存大于二十每次预取二十，小于二十取全部，没有则秒杀失败
            log.info(goods.getStockCount()+"");
            if(goods.getStockCount()>=20){
                //减库存
                int stock=goods.getStockCount();
                boolean flag = seckillGoodsService.update(new UpdateWrapper<SeckillGoods>().setSql("stock_count=" + "stock_count-"+stock).eq("goods_id", goods.getId()).gt("stock_count", stock-1));
                //判断减库存是否成功，预取
                if (flag){
                    log.info("join 0<stock_count");
                    redisTemplate.opsForValue().increment(goodsId+":",goods.getStockCount());
                    Order order = orderService.seckill(user, goods,model);
                    model.addAttribute("order", order);
                    model.addAttribute("goods", goods);
                }
            } else {
                model.addAttribute("errmsg", RespBeanEnum.EMPTY_STOCK.getMessage());
                return "seckillFail";
            }
        }
        return "orderDetail";
    }
}